package isp.apps.example.shoppingcart.action;

import com.opensymphony.xwork.ActionSupport;

import isp.apps.example.shoppingcart.domain.CartItem;
import isp.apps.example.shoppingcart.domain.CompletedOrder;
import isp.apps.example.shoppingcart.domain.Item;
import isp.apps.example.shoppingcart.domain.ShoppingCart;
import isp.apps.example.shoppingcart.domain.ShoppingCartValidator;
import isp.apps.example.shoppingcart.persistence.CartItemsDirectory;
import isp.apps.example.shoppingcart.persistence.factory.CartItemsPersistenceFactory;
import isp.apps.example.shoppingcart.persistence.CompletedOrderDirectory;
import isp.apps.example.shoppingcart.persistence.factory.CompletedOrderPersistenceFactory;

import isp.apps.example.shoppingcart.persistence.ItemDirectory;

import isp.apps.example.shoppingcart.persistence.factory.ItemPersistenceFactory;

import java.math.BigDecimal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class GetOrderDetailsAction extends ActionSupport
{
  private CompletedOrderDirectory myOrderDir = CompletedOrderPersistenceFactory
    .getCompletedOrderDirectory();
  private CartItemsDirectory myCartDir = CartItemsPersistenceFactory
    .getCartItemsDirectory();

  private CompletedOrder myOrder;
  private CartItem myCartItem;
  private Item myItem;
  private Integer myOrderId;
  private Map orderDetailsMap = new HashMap();
  private List orderItems = new ArrayList();
  
  private BigDecimal totalCost = new BigDecimal(0);
  private BigDecimal totalTax = new BigDecimal(0);
  private BigDecimal totalPrice = new BigDecimal(0);

  public GetOrderDetailsAction()
  {
  }

   public String execute()
  {
    validate();
    myOrder = myOrderDir.getOrderById(myOrderId);
    orderItems = myCartDir.getCartItemsByOrder(myOrderId);
       
    for (Iterator i = orderItems.iterator() ; i.hasNext() ;)
    {
      myCartItem = (CartItem)i.next();
      totalCost = totalCost.add(myCartItem.getCost().multiply(new BigDecimal(myCartItem.getQuantity())));
      totalTax = totalTax.add(myCartItem.getTax().multiply(new BigDecimal(myCartItem.getQuantity())));
    }
    
    totalPrice = totalCost.add(totalTax);
    totalPrice = totalPrice.add(myOrder.getShippingCost());
    return SUCCESS;
  }


  public void validate()
  {
    ShoppingCartValidator scv = new ShoppingCartValidator();
    if ((myOrderId = new Integer(scv.checkNumber(myOrderId.toString()))) == null)
    {
      
    }
    
  }


  public void setOrderId(Integer orderId)
  {
    this.myOrderId = orderId;
  }

  public Integer getOrderId()
  {
    return myOrderId;
  }

  public void setOrderDetailsMap(Map orderDetailsMap)
  {
    this.orderDetailsMap = orderDetailsMap;
  }

  public Map getOrderDetailsMap()
  {
    return orderDetailsMap;
  }

  public void setMyOrder(CompletedOrder myOrder)
  {
    this.myOrder = myOrder;
  }

  public CompletedOrder getMyOrder()
  {
    return myOrder;
  }

  public void setOrderItems(List orderItems)
  {
    this.orderItems = orderItems;
  }

  public List getOrderItems()
  {
    return orderItems;
  }


  public BigDecimal getTotalCost()
  {
    return totalCost;
  }


  public BigDecimal getTotalTax()
  {
    return totalTax;
  }
  
  public BigDecimal getTotalPrice()
  {
    return totalPrice;
  }
}
